package com.wxyzz.satserver.visit.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.wxyzz.common.core.entity.User;
import com.wxyzz.common.core.entity.Visit;
import com.wxyzz.common.core.utils.TokenUtils;
import com.wxyzz.common.core.utils.UserUtils;
import com.wxyzz.satserver.user.service.UserService;
import com.wxyzz.satserver.visit.VO.InteractInfoVO;
import com.wxyzz.satserver.visit.VO.VisitVO;
import com.wxyzz.satserver.visit.dao.VisitDao;
import com.wxyzz.satserver.visit.param.AddVisitParam;
import com.wxyzz.satserver.visit.service.VisitService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * --- 加油，努力 ---
 *
 * @Author XYP
 * @Date 2024/11/16
 * @Solgne 从想做到能做
 * @Desc
 */
@Service
public class VisitServiceImpl implements VisitService {

    @Resource
    private VisitDao visitDao;

    @Resource
    private UserService userService;

    /**
     * 添加访问信息
     * @param beVisitUid
     */
    @Override
    public void addVisit(Integer beVisitUid) {
        Visit visit = new Visit();
        Integer visitorId = UserUtils.getUid();
        visit.setVisitorId(visitorId);
        visit.setBeVisitorId(beVisitUid);

        Visit visit1 = visitDao.selectOne(new LambdaQueryWrapper<Visit>()
                .eq(Visit::getVisitorId, visitorId)
                .eq(Visit::getBeVisitorId, beVisitUid));

        // 判断之前是否访问过
        if (visit1 != null) {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

            visitDao.update(null, new LambdaUpdateWrapper<Visit>()
                    .set(Visit::getVisitTime,sdf.format(new Date()))
                    .eq(Visit::getVisitorId, visit1.getVisitorId())
                    .eq(Visit::getBeVisitorId, visit1.getBeVisitorId()));

        } else {

            visitDao.insert(visit);

        }


    }

    /**
     * 查询访问我的用户
     * @return
     */
    @Override
    public List<VisitVO> selectVisitor() {
        Integer uid = UserUtils.getUid();

        List<Visit> visits = visitDao.selectList(new LambdaQueryWrapper<Visit>()
                .eq(Visit::getVisitorId, uid));

        List<VisitVO> visitVOS = new ArrayList<>();

        List<VisitVO> visitVOList = visits.stream().map(item -> {
            VisitVO visitVO = new VisitVO();
            BeanUtils.copyProperties(item, visitVO);

            User user = userService.selectById(item.getVisitorId());
            visitVO.setAvater(user.getAvatar());
            visitVO.setNickName(user.getNickname());

            return visitVO;

        }).collect(Collectors.toList());

        return visitVOList;
    }

    /**
     * 展示哪些用户谁评论了我的动态
      * @return
     */
    @Override
    public InteractInfoVO findInteractInfo() {
        Integer uid = TokenUtils.getUidFromToken();
        InteractInfoVO interactInfoVO = visitDao.selectInteractInfo(uid);
        return interactInfoVO;
    }


}
